﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;

namespace ZyGame
{
    [PathOptions("Assets/Avatar/Config/language.json", PathOptions.Localtion.Packaged)]
    internal class LanguageSetting : Config<LanguageSetting>, ISerializeble
    {
        private static string language_type = "language_en";
        private Dictionary<int, JObject> units = new Dictionary<int, JObject>();
        public bool Deserialized(string json)
        {
            try
            {
                List<JObject> dataList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<JObject>>(json);
                for (int i = 0; i < dataList.Count; i++)
                {
                    JObject item = dataList[i];
                    int id = Convert.ToInt32(item["id_num"]);
                    if (!units.ContainsKey(id))
                    {
                        units.Add(id, item);
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }

        }

        public static void LanguageOptions(string type)
        {
            language_type = type;
        }

        public string Get(int id)
        {
            if (units.TryGetValue(id, out JObject data))
            {
                return Convert.ToString(data[language_type]);
            }
            return id.ToString();
        }

        public string Serialized()
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(units);
        }
    }
}
